package main

import (
	"fmt"
	"runtime"
	"testing"
)

// 对 Go 协程进行基准测试

func main() {
	fmt.Println("os", runtime.GOOS)
	fmt.Println("sync", testing.Benchmark(benchmarkSync).String())
	fmt.Println("buffered", testing.Benchmark(benchmarkBuffered).String())
}

func benchmarkSync(b *testing.B) {
	ch := make(chan int)
	go func() {
		for i := 0; i < b.N; i++ {
			ch <- i
		}
		close(ch)
	}()
	for range ch {

	}
}

func benchmarkBuffered(b *testing.B) {
	ch := make(chan int, 128)
	go func() {
		for i := 0; i < b.N; i++ {
			ch <- i
		}
		close(ch)
	}()
	for range ch {

	}
}
